<?php

namespace App\Console\Commands;

use App\Jobs\SendChangeOrderMessage;
use App\Models\Goods;
use App\Models\GoodsSetmeal;
use App\Models\GoodsSpec;
use App\Models\MemberOther;
use App\Models\Order;
use App\Models\OrderGoods;
use App\Models\OrderLease;
use App\Models\OrderRemark;
use App\Models\OtherOrder;
use App\Models\OtherOrderAllocate;
use App\Models\OtherWithholdLog;
use App\Models\SmsRecord;
use App\Models\SmsTemplate;
use App\Services\AlipayMessageService;
use App\Services\AlipayService;
use App\Services\ChargeService;
use App\Services\EsignService;
use App\Services\OrderService;
use App\Services\WechatService;
use Illuminate\Console\Command;

class CancelOrderCommand extends BaseCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cancelOrder';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '自动取消订单';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    protected function handleExec()
    {
        $this->cancelOrder();
    }

    const TIME_LIMIT = 3; // 单位小时
    const ORDER_NUM_LIMIT = 100; // 一次操作订单数量
    const NOT_HANDEL_SHOP = [

    ];

    /**
     * 请求接口返回内容
     * @param string $url [请求的URL地址]
     * @param string $params [请求的参数]
     * @param int $ipost [是否采用POST形式]
     * @return  string  b百荣
     */
    // static private function Post($PostArry, $request_url)
    // {
    //     $postData = $PostArry;
    //     $postDataString = http_build_query($postData);// 格式化参数
    //     $curl = curl_init(); // 启动一个CURL会话
    //     curl_setopt($curl, CURLOPT_URL, $request_url); // 要访问的地址
    //     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
    //     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
    //     curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
    //     curl_setopt($curl, CURLOPT_POSTFIELDS, $postDataString); // Post提交的数据包
    //     curl_setopt($curl, CURLOPT_TIMEOUT, 60); // 设置超时限制防止死循环返回
    //     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    //
    //     $tmpInfo = curl_exec($curl); // 执行操作
    //     if (curl_errno($curl)) {
    //
    //         $tmpInfo = curl_error($curl);// 捕抓异常
    //     }
    //     curl_close($curl); // 关闭CURL会话
    //     return $tmpInfo; // 返回数据
    // }

    /**
     * 签约未支付自动取消订单
     */
    public function cancelOrder()
    {
        try {
            // 取消订单
            $limit = self::TIME_LIMIT;
            $status = [
                OrderService::ORDER_STATUS_NOT_PAY,
                OrderService::ORDER_STATUS_SIGN_NOCHECK,
                OrderService::ORDER_STATUS_SIGN_NOTPAY,
            ];
            $where = [
                ['create_time', '<=', strtotime("-4 day")],
            ];
            $order = Order::where($where)->whereIn('status', $status)
                ->select('id', 'mini_id', 'uid', 'trade_no', 'rent_total', 'status', 'rights_auth_no', 'alipay_trade_no', 'sign_status', 'flow_id', 'pay_type')
                ->limit(100)
                ->orderBy('id','asc')
                ->get();
            $order = obToArray($order);
            if ($order) {
                foreach ($order as $item) {
                     $status=Order::query()->where('id', $item['id'])->value('status');
                     if($status<=OrderService::ORDER_STATUS_SIGN_NOCHECK){
                         Order::where('id', $item['id'])->update(['status' => OrderService::ORDER_STATUS_CENCEL]);
                         $add = [
                             'uid' => 23,
                             'username' => '自动',
                             'create_time' => time(),
                             'remark' => '超过3天未通过审核或未支付自动取消',
                             'order_id' => $item['id'],
                         ];
                         OrderRemark::query()->insert($add);

                         SendChangeOrderMessage::dispatch( $item['id'],AlipayMessageService::KX_SHOPPING);
                         SendChangeOrderMessage::dispatch( $item['id'],AlipayMessageService::CONSUMER);

                     }
                }
                $this->infoLog('自动取消订单执行成功', $order);
            }
        } catch (\Exception $exception) {
            $this->infoLog('自动取消订单执行失败' . $exception->getMessage());
        }
    }
}
